/** 
  ******************************************************************************
  * @file    main.c
  * @author  SJU
  * @version V1.0
  * @date    2025-08
  * @brief   ?? LED3+Delay
 ******************************************************************************
  * @attention
  * ????:SJU?????????? 
 ******************************************************************************
 **/
	
#include "stm32f10x.h"
#include "bsp_led.h"
#include "bsp_buzzer.h"
#include "systick.h"
#include "bsp_key.h"
#include "SEG_595.h"

void Delay(volatile uint32_t nCount)	 //???????
{
	for(; nCount != 0; nCount--);
}
 
void LED_TASK1(void)
{
	LED_BLUE_OFF();
	LED_GREEN_OFF();
	LED_RED_OFF();
	LED_WHITE_OFF();	
}
void LED_TASK2(void)
{
	LED_BLUE_ON();
	LED_GREEN_ON();
	LED_RED_ON();
	LED_WHITE_ON();	
}
void LED_TASK(void)
{
	 LED_TASK1();SysTick_Delay_Nms(570);
	 LED_TASK2();SysTick_Delay_Nms(430);
}
void BUZZER_TASK(void)
{
	BUZZER_ON;SysTick_Delay_Nms(200);
	BUZZER_OFF;
}
void LED_TASK_nLED(uint8_t nLED,uint16_t T_ON )
{
	if(nLED==1){
		LED_RED_ON();SysTick_Delay_Nms(T_ON);
		LED_RED_OFF();SysTick_Delay_Nms(1000-T_ON);
	}
	if(nLED==2){
		LED_BLUE_ON();SysTick_Delay_Nms(T_ON);
		LED_BLUE_OFF();SysTick_Delay_Nms(1000-T_ON);
	}
	if(nLED==3){
		LED_GREEN_ON();SysTick_Delay_Nms(T_ON);
		LED_GREEN_OFF();SysTick_Delay_Nms(1000-T_ON);
	}
	if(nLED==4){
		LED_WHITE_ON();SysTick_Delay_Nms(T_ON);
		LED_WHITE_OFF();SysTick_Delay_Nms(1000-T_ON);
	}
	
}
void LED_Buzzer_TASK_T(uint16_t T_ON)
{
	BUZZER_ON;SysTick_Delay_Nms(T_ON);
	BUZZER_OFF;SysTick_Delay_Nms(1000-T_ON);
}
void KEY_BSP(void)
{
	if(Read_KEY1()==0)LED_BLUE_ON();
	else LED_BLUE_OFF();
}
uint8_t key1_pressed_flag=0;
uint8_t key2_pressed_flag=0;
uint8_t KEY_Mciro_pressed_flag=0;
void key_state_scan(void)
{
	static uint8_t old_key1=1,old_key2=1,old_KEY_Mciro=1;
	uint8_t now_key1,now_key2,now_KEY_Mciro;
	
	now_key1=Read_KEY1();
	if(old_key1==1 && now_key1==0)
		key1_pressed_flag=1;
		old_key1=now_key1;
	now_key2=Read_KEY2();
	if(old_key2==1 && now_key2==0)
		key2_pressed_flag=1;
		old_key2=now_key2;
	now_KEY_Mciro=Read_KEY_Mciro();
	if(old_KEY_Mciro==1 && now_KEY_Mciro==0)
		KEY_Mciro_pressed_flag=1;
		old_KEY_Mciro=now_KEY_Mciro;
	
}
	
int main(void)
{	
	/* LED ????? */
	LED_GPIO_Config();	
  BUZZER_GPIO_Config();
 KEY_GPIO_Config();
SEG_595_GPIO_Config();	
uint8_t a=6;
	while (1)
	{
	  key_state_scan();
		if(key1_pressed_flag)
		{
			key1_pressed_flag=0;
			a++;  
		}
		key_state_scan();
		if(key2_pressed_flag)
		{
			key2_pressed_flag=0;
			a--;
		}
		if(KEY_Mciro_pressed_flag)
		{
			KEY_Mciro_pressed_flag=0;
			a=0;
		}
		
		if(a>15)
		{
			a=0;
		}
		if(a<0)
		{
		 a=15;
		}
		
		sendonechar_595(a);
		
	}
  }






















